/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import { HvigorSystemPlugin, Project } from '@ohos/hvigor';

import { buildOptionPath } from '../../common/build-option-path-info.js';
import { projectOhosConfigManager } from '../../common/global/project-ohos-config-manager.js';
import { ProjectModel } from '../../model/project/project-model.js';
import { AppOhosConfig } from '../../options/build/app-ohos-config.js';
import { buildOptionManager } from '../../project/build-option/build-mode-manager.js';
import { Clean } from '../../tasks/common/clean.js';
import { TaskNames } from '../../tasks/common/task-names.js';
import { AssembleApp } from '../../tasks/hook/assemble/assemble-app.js';
import { ProjectInspection } from '../../tasks/inspection/project-inspection.js';
import { ProjectTaskService } from '../../tasks/service/project-task-service.js';
import { SyncProject } from '../../tasks/sync/sync-project.js';
import { OhosProjectContextImpl } from '../context/impl/ohos-project-context-impl.js';
import { OhosAppContext, OhosProjectContext } from '../context/plugin-context.js';
import { OhosPluginId } from './ohos-plugin-id.js';
import SyncTask = TaskNames.SyncTask;

/**
 * 对外暴露app级别的接口和任务的plugin
 *
 * @since 2021/12/16
 */
export abstract class AbstractProjectPlugin extends HvigorSystemPlugin {
  // 实际为hvigor的Project对象
  protected _projectName: string;
  protected _projectModel: ProjectModel | undefined;
  protected _taskService: ProjectTaskService | undefined;
  protected ohosAppContext: OhosAppContext | OhosProjectContext | undefined;

  _project: Project;

  assembleApp: AssembleApp | undefined;
  clean: Clean | undefined;
  syncProject: SyncProject | undefined;

  protected constructor(project: Project) {
    super(OhosPluginId.OHOS_APP_PLUGIN);
    this._project = project;
    this._projectName = project.getName();
  }

  abstract initTaskService(): void;

  doRegisterTask() {
    this.assembleApp = new AssembleApp(this._project, this._taskService!);
    this.clean = new Clean(this._project, this._taskService!);
    this.syncProject = new SyncProject(this._project, SyncTask.syncTaskName(this._projectName));

    this._project.registry(this.assembleApp);
    this._project.registry(this.clean);
    this._project.registry(this.syncProject);
  }

  doProjectInspection() {
    const inspection: ProjectInspection = new ProjectInspection(this._projectModel!);
    inspection.doInspection().exitOnError();
  }

  initBuildOptionMap(path: string) {
    buildOptionManager.initProjectBuildOptions(this._projectModel!, path);
    buildOptionPath.initSignConfig(this._projectModel!, path);
  }

  initTraceData(): void {
  }

  initGlobalData() {
    const ohosConfig = this._project.getConfigOpt()?.getObject('ohos') as AppOhosConfig;

    if (!ohosConfig) {
      return;
    }

    projectOhosConfigManager.loaderConfig(ohosConfig);
  }

  public getTaskService(): ProjectTaskService | undefined {
    return this._taskService;
  }

  public getProjectModel(): ProjectModel | undefined {
    return this._projectModel;
  }

  initContext(): void {
    this.ohosAppContext = new OhosProjectContextImpl(this, this._taskService!);
  }

  getContext(): OhosAppContext | undefined {
    return this.ohosAppContext;
  }
}
